/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-08-04 21:31:04
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-10-17 17:13:53
 */
import { Body,Query, Controller, Get, Post, Req, Res, Param, Module,Put, Delete } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { UseGuards } from '@nestjs/common'
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { UsersService } from './users.service';
import { User } from 'src/entity/user.entity';
import {LoginService} from '../login/login.service'
import { UserDetail } from 'src/entity/userDetail.entity';
import { PageInit,PageNation } from 'src/interface/page.interface';
@Controller('users')

export class UsersController {
    constructor(private userRepository: UsersService,private userNew :LoginService) {
        // console.log(this.userRepository)
    }

    /**
     *
     * @param user :用户名称 username:"用户名"
     * @returns
     */
    @UseGuards(JwtAuthGuard)
    @Post('create')
    async create(@Body() user: User) {
        try {
           return  this.userNew.registerUser(user)
        } catch (error) {
            return {
                code: 500,
                msg:error
            }
        }
    }

    @UseGuards(JwtAuthGuard)
    @Get('findOne')
    async findOne(@Body() user: User){
         return this.userRepository.findOne(user.id,user.username);
    }

    @UseGuards(JwtAuthGuard)
    @Get('findAll')
    async findAll(@Query() query:PageInit) {
        // console.log('输出take：',query)
        let res = await this.userRepository.findAll(query);
        return res;
    }

    @UseGuards(JwtAuthGuard)
    @Get('findBy')
    async findBy(@Body() user: User){
         return this.userRepository.findBy(user);
    }

    @UseGuards(JwtAuthGuard)
    @Put('update')
    async update(@Body() user: UserDetail){
        let res = await this.userRepository.updateDetail(user);
        console.log('输出返回结构：', res)
        if (res.affected === 1) {
            return {
                data: '返回成功',
                code:1000,
            }
        } else {
            return {
                 data: '返回失败',
                 code:500,
            }
        }
    }

    @UseGuards(JwtAuthGuard)
    @Post('createDetail')
    async createDetail(@Body() user: UserDetail){
         return this.userRepository.createDetail(user);
    }

    @UseGuards(JwtAuthGuard)
    @Delete('deleteUser')
    async deleteUser(@Body() user: UserDetail){
         return this.userRepository.deleteUser(user.id);
    }


    @Get(':id/detail')
    async getUserDetail(@Param('id') userId: number): Promise<UserDetail> {
         return this.userRepository.findUserDetailByUserId(userId);
    }


}
